<?php

namespace slinks\extensions\action\routing;

use slinks\extensions\action\configuration\IConfiguration;
use slinks\common\util\ArrayProxy;
use slinks\extensions\action\configuration\IActionMapping;
use slinks\core\Config\Loader\Loader;

/**
 * Special Route loader that gets routes from ActionMapping configuration. 
 *
 * @author navid
 */
class ActionMappingRouteLoader extends Loader {

    /**
     * @param IConfiguration $resource the configuration to load routes from.
     * @param type $type 
     */
    public function load($resource, $type = null) {
        // all action mappings are keyed by name which is the same name
        // that will be used for the route so... 
        $actionMappings = $resource->getAllActionMappings();
        
        $routes = array();
        $resources = array();
        
        foreach($actionMappings as $mapping){
            $routes[$mapping->getName()] = $mapping->getRoute();
            $resources[] = $mapping->getResource();
        }
                        
        return new RouteCollection($routes, $resources);
    }

    public function supports($resource, $type = null) {
        return $resource instanceof IConfiguration;
    }

}

?>
